<%--
  Created by IntelliJ IDEA.
  User: 86182
  Date: 2022/9/1
  Time: 18:48
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <!-- 引入 layui.css -->
    <link href="//unpkg.com/layui@2.7.6/dist/css/layui.css" rel="stylesheet">
    <!-- 引入 layui.js -->
    <script src="//unpkg.com/layui@2.7.6/dist/layui.js"></script>
</head>
<body>
<%---------------------------------------修改form表格-------------------------------------------------------------%>
<form class="layui-form" action="" id="updateForm" lay-filter="updateFormFilter" style="display: none"
      enctype="multipart/form-data">
    <div class="layui-form-item" style="display: none">
        <label class="layui-form-label">id</label>
        <div class="layui-input-block">
            <input type="text" name="id" required lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">测试标题</label>
        <div class="layui-input-block" style="width: 300px;">
            <input type="text" name="test_name" required lay-verify="required" placeholder="请输入内容" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">开始时间</label>
        <div class="layui-input-block" style="width: 300px;">
            <input type="text" name="test_begin" id="test_begin" required lay-verify="required" placeholder="请输入内容" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">结束时间</label>
        <div class="layui-input-block" style="width: 300px;">
            <input type="text" name="test_end"  id="test_end" required lay-verify="required" placeholder="请输入内容" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block" style="width: 300px;">
            <input type="text" name="remark" required lay-verify="required" placeholder="请输入内容" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="updateFilter">确认修改</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<%---------------------------------------添加form表格----------------------------------------------------%>
<form class="layui-form" action="" id="addForm" lay-filter="addFormFilter" style="display: none"
      enctype="multipart/form-data">
    <div class="layui-form-item">
        <label class="layui-form-label">测试标题</label>
        <div class="layui-input-block" style="width: 300px;">
            <input type="text" name="test_name" id="questionAdd" required lay-verify="required" placeholder="请输入内容" autocomplete="off"
                   class="layui-input">
            <span style="color: red"></span>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">开始时间</label>
        <div class="layui-input-block" style="width: 300px;">
            <input type="text" name="test_begin" id="beginTime" required lay-verify="required"  autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">结束时间</label>
        <div class="layui-input-block" style="width: 300px;">
            <input type="text" name="test_end"  required lay-verify="required" id="endTime" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block" style="width: 300px;">
            <input type="text" name="remark" required lay-verify="required" placeholder="请输入内容" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn"  lay-submit lay-filter="addFilter">确认添加</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<%---------------------------------------条件查询--------------------------------------------------------%>
<form class="layui-form" id="haha" action="" style="margin-top: 10px">
    <div class="layui-inline">
        <label class="layui-form-label">测试名称</label>
        <div class="layui-input-inline">
            <input type="text" name="test_name" placeholder="请输入测试名称" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">创建人</label>
        <div class="layui-input-inline">
            <input type="text" name="create_by" placeholder="请输入创建人" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <div class="layui-input-inline">
            <button class="layui-btn" lay-submit lay-filter="conditionalQuery">查询 <i
                    class="layui-icon layui-icon-search" style="font-size: 20px;"></i>
            </button>
            <button type="test" class="layui-btn layui-btn-primary" id="reset">重置</button>
        </div>
    </div>
</form>
<%--------------------------------------顶部功能框--------------------------------------------------------%>
<script type="text/html" id="barDemo1">
    <a class="layui-btn " type="add" lay-event="add"><i class="layui-icon layui-icon-add-circle"
                                                        style="font-size: 20px;"></i>添加</a>
    <a class="layui-btn layui-btn-danger" lay-event="del"><i class="layui-icon layui-icon-reduce-circle"
                                                             style="font-size: 20px;"></i>删除</a>
</script>
<%-------------------------------------右侧操作框------------------------------------------------------%>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs layui-btn" lay-event="detail">查看二维码</a>
    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="update">编辑</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger"  lay-event="delete">删除</a>
</script>
<%--二维码--%>
<div id="qrcode" style="width:150px; height:150px; display: none; margin-left: 50px;margin-top: 20px"></div>
<table id="demo" lay-filter="test"></table>
<script src="/layui/layui.js"></script>
<script src="/static/build/layui.js"></script>
<script>
    layui.use(['table','form','jquery','laydate'], function(){
        var table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        var $ = layui.jquery;
        //第一个实例
        table.render({
            elem: '#demo'
            ,height:600
            ,url: '${pageContext.request.contextPath}/testpfa/queryPersonalityTestPlanAll.do' //数据接口
            ,page: true //开启分页
            ,limit:10
            ,limits:[10,20,30,40,50,60,70]
            ,toolbar: '#barDemo1'
            ,cols: [[ //表头
                {type: 'checkbox', fixed: 'left'}
                , {field: 'id', title: 'ID', width:120, sort: true, fixed: 'left'}
                , {field: 'test_name', title: '项目名称', width: 120}
                , {field: 'create_by', title: '创建人', width:120}
                , {field: 'test_code', title: '测试邀请码', width: 120, sort: true}
                , {field: 'test_begin', title: '测试开始时间', width: 180}
                , {field: 'test_end', title: '测试结束时间', width: 180},
                , {field: 'remark', title: '测试结果', width: 150, sort: true}
                , {field: 'create_time', title: '测试创建时间', width: 180, sort: true}
                , {field: 'update_time', title: '修改时间', width: 180, sort: true}
                , {field: 'update_by', title: '修改人', width: 100}
                , {field: 'remark', title: '备注', width: 100}
                , {fixed: 'right', title: '操作', width: 240, align: 'center', toolbar: '#barDemo'}
            ]]
        });
        /**
         * 添加
         */
        form.on('submit(addFilter)', function(data){
            console.log(data.elem) //被执行事件的元素DOM对象，一般为button对象
            console.log(data.form) //被执行提交的form对象，一般在存在form标签时才会返回
            console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            $.ajax({
                url:"${pageContext.request.contextPath}/testpfa/addPersonalityTestPlan.do",
                type:"POST",
                dataType:"JSON",
                data:data.field,
                success:function (ret){
                    if(ret.code==0){
                        layer.closeAll();//关闭对话
                        table.reload('demo',{
                            page: {

                                curr: 1 // 重新加载 从第一页开始
                            }
                        });
                    }
                    layer.msg(ret.msg);
                }
            })
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。

        });

        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id)
                ,data = checkStatus.data; //获取选中的数据
            switch(obj.event) {
                case 'add':
                    //  添加表单
                    layer.open({
                        title:'添加技术',
                        type: 1,
                        content: $('#addForm') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                        ,area:['500px','300px']
                    });
                    laydate.render({
                        elem: '#beginTime'
                        , type: 'datetime'
                        , value: new Date()
                        , isInitValue: true
                    });
                    var now = new Date().getTime();
                    laydate.render({
                        elem: '#endTime'
                        , type: 'datetime'
                        , value: new Date(now + 2 * 86400 * 1000)
                        , isInitValue: true
                    });
                    break;
                /**
                 *  多行删除
                 */
                case 'del':
                    if(data.length === 0){
                        layer.msg('请选择一行');
                    }else {
                        var ids = [];
                        for(let i = 0 ; i < data.length;i++) {
                            ids[i] =data[i].id;
                        }
                        layer.confirm('确认删除吗', function (index) {
                            $.ajax({
                                url: "${pageContext.request.contextPath}/testpfa/deletequeryPersonalityTestPlan.do",
                                type: "POST",
                                dataType: "JSON",
                                data: {id: ids.join(",")},
                                success: function (data) {
                                    if (data.code == 0) {
                                        layer.closeAll();
                                        table.reload('demo');
                                    }
                                    layer.msg(data.msg);
                                }
                            });
                        })
                    }
                    break;
            }

        });
        /**
         * 单行删除和修改
         */
        form.on('submit(updateFilter)', function(data){
            console.log(data.elem) //被执行事件的元素DOM对象，一般为button对象
            console.log(data.form) //被执行提交的form对象，一般在存在form标签时才会返回
            console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            $.ajax({
                url:"${pageContext.request.contextPath}/testpfa/updatePersonalityTestPlan.do",
                type:"POST",
                dataType:"JSON",
                data:data.field,
                success:function (ret){
                    if(ret.code==0){
                        layer.closeAll();//关闭对话
                        table.reload('demo',{
                            page: {

                                curr: 1 // 重新加载 从第一页开始
                            }
                        });
                    }
                    layer.msg(ret.msg);
                }
            })
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        //监听行工具事件
        table.on('tool(test)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'update') {
                //updateFormFilter
                console.log("data.id")
                console.log(data.id)
                console.log(data)
                form.val("updateFormFilter", { //formTest 即 class="layui-form" 所在元素属性 lay-filter="" 对应的值
                    "id":data.id
                    ,"test_name": data.test_name // "name": "value"
                    ,"option_c": data.option_c
                    ,"remark": data.remark
                    ,"typ": data.typ
                    ,"status": data.status
                    ,"flag": data.flag
                });
                //日期回显
                laydate.render({
                    elem: '#test_begin'
                    , type: 'datetime'
                    , value:data.test_begin
                    , isInitValue: true
                });
                laydate.render({
                    elem: '#test_end'
                    , type: 'datetime'
                    , value:data.test_end
                    , isInitValue: true
                });
                // 修改表单
                layer.open({
                    type: 1,
                    content: $('#updateForm') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                    , area: ['500px', '300px']
                });
            } else if (layEvent === 'delete') {
                layer.confirm('确认删除吗', function (index) {
                    //去后台批量删除数据
                    $.ajax({
                        url: "${pageContext.request.contextPath}/testpfa/deletequeryPersonalityTestPlan.do",
                        type: "POST",
                        dataType: "JSON",
                        data: {id: data.id},
                        success: function (data) {
                            if (data.code == 0) {
                                //关闭对话框
                                layer.closeAll(); //疯狂模式，关闭所有层
                                //刷新表格数据
                                table.reload('demo');

                            }
                            layer.msg(data.msg);
                        }

                    });
                });
            }else if(layEvent === 'detail'){
                // 二维码
                layer.open({
                    title: '查看二维码',
                    skin: "layui-layer-molv",//弹出层=皮肤样式
                    type: 1,
                    content: $("#qrcode"),
                    area: ['300px', '300px']
                })
            }
        })
        /**
         * 模糊查询
         */
        form.on('submit(conditionalQuery)', function(data){
            console.log(data.elem) //被执行事件的元素DOM对象，一般为button对象
            console.log(data.form) //被执行提交的form对象，一般在存在form标签时才会返回
            console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            table.reload('demo', {
                where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                    test_name: data.field.test_name //排序字段
                    ,create_by:data.field.create_by//排序方式
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        // 重置条件查询表单,刷新table数据
        $("#reset").click(function () {
            myTable.reload({
                where:{ // 设定异步数据接口的额外参数,任意设置
                    test_name:""
                    ,create_by:""
                }
            })
        })
        //二维码渲染
        layui.config({                  //配置layui插件
            base: '${pageContext.request.contextPath}/static/layui/lay/modules/' //配置 layui 第三方扩展组件存放的  目录（不包含文件名）
        }).extend({
            qrcode: 'qrcode'        //前边是方法名，后边是文件名
        }).use(['qrcode'], function () {   //使用插件
            var $ = layui.$;
            $('#qrcode').qrcode({           //调用方法
                //这两个属性必须写
                render: "canvas",  //table or canvas     二维码生成的两种方式 canvas是生成图片 table是黑白格子组成的二维码
                text: "http://192.168.41.238:8080/${pageContext.request.contextPath}/views/front/testLogin.jsp",  //二维码的内容
                //宽高和颜色可以不设置
                width: 200,
                height: 200,
                foreground: "#000000",  //二维码上层颜色
                background: "#FFF"      //背景颜色
            });
        })
        /**
         * 测试项目名称唯一性校验
         */
        $("#questionAdd").blur(function (){
            $.ajax({
                url:"${pageContext.request.contextPath}/testpfa/repetitionName.do",
                data: {question: $("#questionAdd").val()},
                type:"GET",
                dataType:"JSON",
                success: function (re) {
                    if (re.code == 0) {
                        $("#questionAdd").next().html(re.msg);
                        $('#addQuestion').removeClass("layui-btn-disabled").attr("disabled", false);
                    } else {
                        $("#questionAdd").next().html(re.msg);
                        $('#addQuestion').addClass("layui-btn-disabled").attr("disabled", true);
                    }
                }
            })

        })
    });
</script>

</body>
</html>
